package com.uade.tp.view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;

import com.uade.tp.controlador.Delivery;
import com.uade.tp.negocio.view.RecetaView;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class BajaReceta extends javax.swing.JFrame {
	private JComboBox jComboBox1;
	private JLabel jLabel8;
	private JPanel jPanel1;
	private JButton jButton8;
	private JLabel jLabel12;
	private JLabel jLabel11;
	private JButton jButton5;
	private static BajaReceta instancia;
	private JTextPane texto;

	private DefaultListModel listaRecetas;

	
	public static BajaReceta getInstancia() {
		if (instancia == null){
			instancia = new BajaReceta();
		}
		instancia.setLocationRelativeTo(null);
		instancia.limpiar();
		instancia.texto.setText("");
		instancia.texto.setEditable(false);
		return instancia;
		
	}
	
	private BajaReceta() {
		super();
		initGUI();
	}
	
	
	private void limpiar(){

		listaRecetas = new DefaultListModel();
		jComboBox1.removeAllItems();
		Vector <RecetaView> recetas = Delivery.getInstancia().getDeliveryView().obtenerRecetasView();
		for(RecetaView r:recetas){
			jComboBox1.addItem(r);
		}
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("Baja de Receta");
			getContentPane().setLayout(null);
			{
				jPanel1 = new JPanel();
				getContentPane().add(jPanel1);
				jPanel1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
				jPanel1.setLayout(null);
				jPanel1.setBounds(12, 34, 308, 91);
				{
					jLabel8 = new JLabel();
					jPanel1.add(jLabel8);
					jLabel8.setText("Receta :");
					jLabel8.setBounds(14, 14, 55, 16);
				}
				{
					ComboBoxModel jComboBox1Model = 
							new DefaultComboBoxModel();
					jComboBox1 = new JComboBox();
					jPanel1.add(jComboBox1);
					jComboBox1.setModel(jComboBox1Model);
					jComboBox1.setBounds(87, 11, 207, 23);
				}
				{
					jButton5 = new JButton();
					jPanel1.add(jButton5);
					jButton5.setText("Eliminar");
					jButton5.setBounds(199, 46, 96, 23);
					jButton5.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							try{
								texto.setText("");
								if (!Delivery.getInstancia().tieneRecetaHijo(((RecetaView)jComboBox1.getSelectedItem()).getNombre())){
									Delivery.getInstancia().bajaReceta(((RecetaView)jComboBox1.getSelectedItem()).getNombre());
									limpiar();
									texto.setText("Receta dada de baja");
								}else{
									texto.setText("Receta NO dada de bajar por ser parte de compuesta");
								}
							}catch(NullPointerException e) {
								JOptionPane.showMessageDialog(null, "Debe Seleccionar un elemento");
							}
						}
					});
				}
				{
					jLabel11 = new JLabel();
					jPanel1.add(jLabel11);
					jLabel11.setText("Listado Productos");
					jLabel11.setBounds(29, 212, 95, 16);
				}
			}
			{
				jLabel12 = new JLabel();
				getContentPane().add(jLabel12);
				jLabel12.setText("Recetas");
				jLabel12.setBounds(12, 12, 65, 16);
			}
			{
				jButton8 = new JButton();
				getContentPane().add(jButton8);
				getContentPane().add(getTexto());
				jButton8.setText("Salir");
				jButton8.setBounds(245, 137, 75, 23);
				jButton8.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						System.out.println("jButton8.actionPerformed, event="+evt);
						limpiar();
						instancia.setVisible(false);
					}
				});
			}
			pack();
			this.setSize(366, 218);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private JTextPane getTexto() {
		if(texto == null) {
			texto = new JTextPane();
			texto.setBounds(17, 131, 207, 48);
		}
		return texto;
	}

}
